package 题目集.并查集.带权并查集;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

//https://www.acwing.com/problem/content/242/
public class 食物链 {
    static int n, k;
    static int[] p;
    static int[] d;

    public static void main(String[] args) {
        Reader sc = new Reader();
        n = sc.nextInt();
        k = sc.nextInt();
        p = new int[n + 1];
        d = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            p[i] = i;
        }
        long res = 0;
        for (int i = 0; i < k; i++) {
            int op = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (a > n || b > n) {
                res++;
                continue;
            }
            if (!union(a, b, op - 1)) {  //op-1是该操作对应的关系。0：相等，1：a吃b，2：b吃a
                switch (op) {
                    case 1:
                        if (d[a] != d[b]) {
                            res++;
                        }
                        break;
                    case 2:
                        if ((d[a] - d[b] + 3) % 3 != 1) {
                            res++;
                        }
                        break;
                }
            }
        }
        System.out.println(res);
    }

    /**
     * a到b的关系是b，该方法不具有交换律。
     * 如果a和b本就在同一个集合中，返回false
     */
    public static boolean union(int a, int b, int t) {
        int fa = find(a);
        int fb = find(b);
        if (fa == fb) {
            return false;
        }
//        d[fa] = (d[b] - d[a] - 1 + 30) % 3;   //a->b的关系，但不是相等的关系（a->b!=b->a），所以这里从b推a的关系是错误的
        d[fa] = (d[b] - d[a] + t + 3) % 3;
        p[fa] = fb;
        return true;
    }

    public static int find(int x) {
        if (p[x] == x) {
            return x;
        }
        int fx = find(p[x]);    //返回新的root节点
        d[x] = (d[x] + d[p[x]]) % 3;    //d[px]：原root节点到新root节点的距离
        p[x] = fx;
        return fx;
    }

    static class Reader {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
